package guava;

import com.google.common.collect.Lists;
import com.google.common.collect.TreeTraverser;
import com.google.common.io.Files;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Test;
import org.xukai.common.CvsUtil;
import org.xukai.common.ProductReport;
import org.xukai.common.util.DatetimeUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author xukai
 * @desc
 * @date 2017-04-07-8:38
 */
public class IOUtil {


    @Test
    public void testFiles() throws IOException {
        File file = new File("D:\\java\\IDEA-Workspace\\xukai_basic\\basic-common\\src\\test\\java\\guava" +
                "\\ReflectUtil.java");
        File root = new File("D:\\java\\IDEA-Workspace\\xukai_basic\\basic-common\\src\\main\\java\\org\\xukai\\common");
        byte[] bytes = Files.toByteArray(file);
        System.out.println(bytes.length);
        MappedByteBuffer map = Files.map(file, FileChannel.MapMode.READ_WRITE);
        Files.createParentDirs(file);
        TreeTraverser<File> traverser = Files.fileTreeTraverser();
        Iterable<File> it = traverser.postOrderTraversal(root);
        for(File children : it){
            System.out.println(children.getAbsolutePath());
        }
    }

    @Test
    public void testFiles2() throws IOException {
        FileInputStream in = new FileInputStream("C:\\Users\\002117030044\\Desktop\\amazon\\test.xml");
        Scanner scanner = new Scanner(in).useDelimiter("\t");
        int count = 0;
        while (scanner.hasNext()) {
            String next = scanner.next();

            if (StringUtils.isNotBlank(next)) {
                System.out.println(count + " = " + next);
                count++;
            }
        }
        System.out.println(count);
    }

    @Test
    public void testToken() throws IOException {
        File file = new File("C:\\Users\\002117030044\\Desktop\\amazon\\_GET_MERCHANT_LISTINGS_ALL_DATA_report.xml");
        byte[] bytes = Files.toByteArray(file);
        List<ProductReport> list = CvsUtil.cvs2BeanList(bytes, ProductReport.class);
        System.out.println(list.toString());
    }

    @Test
    public void testDelete() throws IOException {
        String rootPath = "D:/data/tmp/oozie";
        Date start = DatetimeUtils.getDateByFormat("2018-01-01 01:03:00", "yyyy-MM-dd HH:mm:ss");
        remove(2, rootPath, start, new Date(), TimeUnit.DAY);

    }

    private void remove(int level, String path, Date downLimit, Date upLimit,TimeUnit compareTimeUnit) {

        _remove(level, path, downLimit, upLimit, compareTimeUnit);
//        _remove2(path, downLimit, upLimit, 0);
    }

    private boolean _remove2(String path, Date downLimit, Date upLimit,int i) {
        if (i == 5) {
            System.out.println("删除。。" + path);
        }
        Calendar down = Calendar.getInstance();
        Calendar up = Calendar.getInstance();
        down.setTime(downLimit);
        up.setTime(upLimit);
        String[] subPaths = new File(path).list();
        for (String subPath : subPaths) {
            if (Integer.parseInt(subPath) > down.get(timeUnitSeq.get(i)) && Integer.parseInt(subPath) < up.get
                    (timeUnitSeq.get(i))) {
                System.out.println("删除。。" + path + "/" + subPath);
            } else if (Integer.parseInt(subPath) == down.get(timeUnitSeq.get(i)) || Integer.parseInt(subPath) == up.get
                    (timeUnitSeq.get(i))) {
                i++;
                if (_remove2(path + "/" + subPath, downLimit, upLimit, i)) {
                    System.out.println("删除。。" + path + "/" + subPath);
                }
            }
        }
        String[] afterDeleteSubPaths = new File(path).list();
        return afterDeleteSubPaths.length == 0;
    }

    private boolean _remove(int level, String path, Date downLimit, Date upLimit,TimeUnit compareTimeUnit) {
        if (compareTimeUnit == null) {
            return true;
        }
        String[] subPaths = new File(path).list();
        if (level == 0) {
            for (String subPath : subPaths) {
                Date date = path2Calendar(path + "/" + subPath);
                Date compareUpLimit = new Date();
                compareUpLimit = compareUpLimit.before(upLimit) ? compareUpLimit : upLimit;
                Date compareDownLimit = new Date();
                if (TimeUnit.YEAR.equals(compareTimeUnit)) {
                    compareDownLimit = DateUtils.truncate(downLimit, Calendar.YEAR);
                    compareUpLimit = DateUtils.truncate(compareUpLimit, Calendar.YEAR);
                } else if (TimeUnit.MONTH.equals(compareTimeUnit)) {
                    compareDownLimit = DateUtils.truncate(downLimit, Calendar.MONTH);
                    compareUpLimit = DateUtils.truncate(compareUpLimit, Calendar.MONTH);
                } else if (TimeUnit.DAY.equals(compareTimeUnit)) {
                    compareDownLimit = DateUtils.truncate(downLimit, Calendar.DAY_OF_MONTH);
                    compareUpLimit = DateUtils.truncate(compareUpLimit, Calendar.DAY_OF_MONTH);
                } else if (TimeUnit.HOUR.equals(compareTimeUnit)) {
                    compareDownLimit = DateUtils.truncate(downLimit, Calendar.HOUR_OF_DAY);
                    compareUpLimit = DateUtils.truncate(compareUpLimit, Calendar.HOUR_OF_DAY);
                } else if (TimeUnit.MINUTE.equals(compareTimeUnit)) {
                    compareDownLimit = DateUtils.truncate(downLimit, Calendar.MINUTE);
                    compareUpLimit = DateUtils.truncate(compareUpLimit, Calendar.MINUTE);
                }
                if (date.after(compareDownLimit) && date.before(compareUpLimit)) {
                    System.out.println("删除。。" + path + "/" + subPath);
                } else if ((date.compareTo(compareDownLimit) == 0 || date.compareTo(compareUpLimit) == 0) && _remove
                        (0, path + "/" + subPath, downLimit, upLimit, getNextLevelTimeUnit(compareTimeUnit))) {
                    System.out.println("删除。。" + path + "/" + subPath);
                }
            }

        } else {
            level -= 1;
            for (String subPath : subPaths) {
                if (_remove(level, path + "/" + subPath, downLimit, upLimit, compareTimeUnit)) {
                    System.out.println("删除。。" + path + "/" + subPath);
                }
            }
        }

        String[] afterDeleteSubPaths = new File(path).list();
        return afterDeleteSubPaths.length == 0;
    }

    private TimeUnit getNextLevelTimeUnit(TimeUnit timeUnit){
        if (TimeUnit.YEAR.equals(timeUnit)) {
            return TimeUnit.MONTH;
        } else if (TimeUnit.MONTH.equals(timeUnit)) {
            return TimeUnit.DAY;
        } else if (TimeUnit.DAY.equals(timeUnit)) {
            return TimeUnit.HOUR;
        } else if (TimeUnit.HOUR.equals(timeUnit)) {
            return TimeUnit.MINUTE;
        } else if (TimeUnit.MINUTE.equals(timeUnit)) {
            return null;
        }
        return null;
    }

    private List<Integer> timeUnitSeq = Lists.newArrayList(Calendar.YEAR, Calendar.MONTH, Calendar.DATE, Calendar
            .HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND);

    private Date path2Calendar(String path) {
        String[] paths = StringUtils.split(path, "/");
        Calendar calendar = Calendar.getInstance();
        int j = 0;
        for (int i = 4; i < paths.length; i++) {
            calendar.set(timeUnitSeq.get(j), i == 5 ? Integer.valueOf(paths[i]) - 1 : Integer.valueOf(paths[i]));
            j += 1;
        }
        return DateUtils.truncate(calendar, timeUnitSeq.get(j - 1)).getTime();
    }


}
